package com.situ.blog.controller;


import com.situ.blog.pojo.entity.Blog;
import com.situ.blog.pojo.entity.BlogType;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.pojo.vo.BlogVO;
import com.situ.blog.service.IBlogService;
import com.situ.blog.service.IBlogTypeService;
import com.situ.blog.service.impl.BlogServiceImpl;
import com.situ.blog.service.impl.BlogTypeServiceImpl;
import com.situ.blog.util.MyBeanUtils;
import com.situ.blog.util.PageInfo;
import com.situ.blog.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/blog")
//不在继承HttpServlet，而是继承BaseController,可以简化toJSON()
public class BlogController extends BaseController {
    private IBlogService blogService = new BlogServiceImpl();
    private IBlogTypeService blogTypeService = new BlogTypeServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");

        switch (method){
            case "selectByPage"://从分页做搜索功能
                selectByPage(req,resp);
                break;
            case "selectBlogCount": //饼状图
                selectBlogCount(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "deleteAll":
                deleteAll(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "selectById":
                selectById(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
            case "updateStatus":
                updateStatus(req,resp);
                break;
            case "exportExcel": //导出Excel
                exportExcel(req,resp);
                break;
            case "importExcel":
                importExcel(req,resp);
                break;

        }
    }

    private void importExcel(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogController.importExcel");
        blogService.importExcel(req);
        toJSON(resp,Result.ok("上传成功"));
    }

    private void exportExcel(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogController.exportExcel");
        blogService.exportExcel(resp);
    }

    private void selectBlogCount(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogController.selectBlogCount");
        BlogQuery blogQuery = MyBeanUtils.copyToBean(req,BlogQuery.class);
        List<BlogVO> list = blogService.selectBlogCount(blogQuery);
        toJSON(resp,Result.ok(list));

    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogController.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        blogService.updateStatus(Integer.parseInt(id),Integer.parseInt(status));
        toJSON(resp,Result.ok("更新成功"));


    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.update");

        //Map<String, String[]> parameterMap = req.getParameterMap();
        //id: 1
        //name: zhangsan
        //content: shdishd
        /*String id = req.getParameter("id");
        String name = req.getParameter("name");
        String content = req.getParameter("content");
        Blog blog = new Blog();
        blog.setId(Integer.parseInt(id));
        blog.setName(name);
        blog.setContent(content);*/

        //使用MyBeanUtils工具把封装的对象进行简化
        Blog blog = MyBeanUtils.copyToBean(req,Blog.class);
        blogService.update(blog);

        //JSONUtils.toJSON(resp, Result.ok("更新成功"));
        toJSON(resp, Result.ok("更新成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.selectById");
        String id = req.getParameter("id");
        Blog blog = blogService.selectById(Integer.parseInt(id));
        List<BlogType> list = blogTypeService.selectAll();
        //JSONUtils.toJSON(resp,Result.ok(blog));
        Map<String,Object> map = new HashMap<>();
        map.put("blog",blog);
        map.put("list",list);

        toJSON(resp,Result.ok(map));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.add");
        /*String name = req.getParameter("name");
        String content = req.getParameter("content");
        Blog blog = new Blog();
        blog.setName(name);
        blog.setContent(content);*/

        Blog blog = MyBeanUtils.copyToBean(req,Blog.class);

        blogService.add(blog);

        //JSONUtils.toJSON(resp,Result.ok("添加成功"));
        toJSON(resp,Result.ok("添加成功"));

    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        blogService.deleteAll(ids);
        //JSONUtils.toJSON(resp,Result.ok("删除成功"));
        toJSON(resp,Result.ok("删除成功"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.deleteById");
        String id = req.getParameter("id");
        blogService.deleteById(Integer.parseInt(id));
        //JSONUtils.toJSON(resp, Result.ok("删除成功"));
        toJSON(resp, Result.ok("删除成功"));
    }

    //分页、搜索框
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.selectByPage");

        /*String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        BlogQuery blogQuery = new BlogQuery(Integer.parseInt(page),Integer.parseInt(limit),name);*/

        BlogQuery blogQuery = MyBeanUtils.copyToBean(req,BlogQuery.class);
        PageInfo pageInfo = blogService.selectByPage(blogQuery);
        //PageInfo layUITableResult = blogService.selectByPage(Integer.parseInt(page),Integer.parseInt(limit));

        //JSONUtils.toJSON(resp,pageInfo);
        toJSON(resp,pageInfo);

    }
}
